﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections.ObjectModel;
using Ones.Models;

using TweetSharp.Twitter.Service;
using TweetSharp.Twitter.Model;
using TweetSharp;
using TweetSharp.Model;

using System.Diagnostics;
using System.IO;
using System.Net;


namespace Ones.ViewModels
    {
    class TwitterViewModel
        {
        private ObservableCollection<TwitterStatus> _tweets = new ObservableCollection<TwitterStatus>();

        public ObservableCollection<TwitterStatus> Tweets
            {
                get { return _tweets; }
            }

            public void LoadTweetStream(TwitterService service)
            {
                service.StreamResult += new EventHandler<TwitterStreamResultEventArgs>(service_StreamResult);

                TwitterFilterStreamOptions options = new TwitterFilterStreamOptions();

                options.TrackingKeywords.Add("Manaus");
                                
                IAsyncResult result = service.BeginStreamFirehose(10);
            }

            void service_StreamResult(object sender, TwitterStreamResultEventArgs e)
            {
                foreach (TwitterStatus tweet in e.Statuses)
                {
                    _tweets.Add(tweet);
                }
            }


            public void LoadTweets(TwitterService service)
            {
                IEnumerable<TwitterStatus> tweets;

                if (_tweets.Count == 0)
                {
                    tweets = service.ListTweetsOnHomeTimeline();
                    CreateFirstTweets(tweets);

                }
                else
                {
                    TwitterStatus last = _tweets.ElementAt<TwitterStatus>(0);
                    tweets = service.ListTweetsOnHomeTimelineSince(last.Id);
                    InsertLastTweets(tweets);
                }
            }

            public void LoadMentions(TwitterService service)
            {
                IEnumerable<TwitterStatus> tweets;
                
                if (_tweets.Count == 0)
                {
                    tweets = service.ListTweetsMentioningMe();
                    CreateFirstTweets(tweets);
                }
                else
                {
                    TwitterStatus last = _tweets.ElementAt<TwitterStatus>(0);
                    tweets = service.ListTweetsMentioningMeSince(last.Id);
                    InsertLastTweets(tweets);
                }
            }

            private void CreateFirstTweets(IEnumerable<TwitterStatus> tweets)
            {
                foreach (var tweet in tweets)
                {
                    _tweets.Add(tweet);
                }
            }

            private void InsertLastTweets(IEnumerable<TwitterStatus> tweets)
            {
                foreach (var tweet in tweets)
                {
                    _tweets.Insert(0, tweet);
                }
            }
        }
    }
